<template>
	<view class="inventoryTask">
		<!-- 入库任务 -->
		<view class="inventoryTask-model1">
			<view class="inventoryTask-model1-title">标签(查出)</view>
			<view class="inventoryTask-model1-tags" v-for="(item,index) in epcs">
				{{item}}
			</view>
		</view>
		<view class="inventoryTask-model1">
			<view class="inventoryTask-model1-title">托盘标签(扫出)</view>
			<view class="inventoryTask-model1-tags" v-for="(item,index) in strogeEpcList">
				{{item}}
			</view>
		</view>
		
		<view class="inventoryTask-model1">
			<view class="inventoryTask-model1-title">货品标签(扫出)</view>
			<view class="inventoryTask-model1-tags" v-for="(item,index) in productEpcList">
				{{item}}
			</view>
		</view>
		<view class="inventoryTask-model1">
			<view class="inventoryTask-model1-title">货品列表</view>
			<view class="inventoryTask-model1-tags" v-for="(list, index) in groups">
				{{ prefixArray[index].prefixName }}：{{ list.length }}
			</view>
		</view>
		<infoScan @newItem="addItem"></infoScan>
<!-- 		<view class="footerBtn" @click="confirmInfo">
			提交
		</view> -->
	</view>
</template>

<script>
	import{getEpcNumToInfo} from '@/api/admin.js'
	import infoScan from '@/components/infoScan/infoScan.vue'
	export default {
		data() {
			return {
				isShowArea:false,
				trayName:'托盘1',
				detailList:['','',''],
				productEpcList:[],
				strogeEpcList:[],
				epcs:[],
				groups: [],  // 用数组存储分组数据
				prefixArray: [],  // 存储前缀和索引
				nextIndex: 0,  // 下一个可用的索引
			}
		},
		components:{
			  infoScan,
		},
		onLoad(options) {
			if(options.itemDetail){
				let str = decodeURIComponent(options.itemDetail);
				let item = JSON.parse(str);
				this.content = item;
			}
		},
		onShow() {
			
		},
		methods: {
			confirmInfo(){
				
			},
			hexToText(hexString) {
			    let text = '';
			    for (let i = 0; i < hexString.length; i += 2) {
			        let hex = hexString.substring(i, i + 2);
			        text += String.fromCharCode(parseInt(hex, 16));
			    }
			    return text;
			},
			addItem(item){
				let epcString = this.hexToText(item.epc)
			          if (/^TP/.test(epcString)) {
			              // 如果 epc 以 "TP" 开头，执行相应的逻辑
			              console.log('TP logic:', item);
						  this.strogeEpcList.push(epcString)
						  // this.epcs.push(epcString)
			              // 在这里添加你需要的逻辑
			          } else if (/^HP/.test(epcString)) {
			              // 如果 epc 以 "HP" 开头，执行相应的逻辑
			              console.log('HP logic:', item);
						  this.productEpcList.push(epcString)
						  this.hpFind(item);
			              // 在这里添加你需要的逻辑
			          } else {
			              // 如果 epc 不以 "TP" 或 "HP" 开头，可以执行其他逻辑或忽略
			              // console.log('Other logic:', item);
			              // 在这里添加你需要的逻辑
			          }
			},
			changeTray(){
				this.trayName = e.value[0];
			},
			hpFind(data){
				const prefix = data.epc.substring(0, 16);  // 获取前10位字符
				// console.log(prefix)
				let index = this.prefixArray.findIndex(item => item.prefixName === prefix)
				 if (index==-1) {
				    this.$set(this.groups, this.nextIndex, [data]); // 如果前缀不存在，初始化数组并添加第一个元素
					let epcString = this.hexToText(data.epc)
					console.log("epcString",epcString)
					getEpcNumToInfo({'epcNum':epcString}).then(res=>{
						console.log(res)
						if(res.code==200){
							let p = {
								'prefixName':res.data.baseMat.matName,
								'index':index
							}
							this.$set(this.prefixArray, this.nextIndex, p);
							this.nextIndex+=1;
						}
					})
				
				  } else {
				    this.groups[index].push(data); // 如果前缀已存在，直接添加元素到数组
				  }
			}
		}
	}
</script>

<style lang="scss">
	page{background:#F2F2F2 ;width:100%;}
	.inventoryTask{
		padding-bottom:180rpx;
		&-model1{
			width:690rpx;
			background:white;
			min-height:250rpx;
			margin:0 auto;
			margin-top:30rpx;
			border-radius: 24rpx;
			padding:0 20rpx;
			overflow: hidden;
			padding-bottom: 20rpx;;
			&-title{
				margin-top:20rpx;
				color:#007aff;
				font-weight: 600;
				font-size: 34rpx;
			}
			&-tags{
				font-weight: 400;
				font-size: 28rpx;
				margin-top: 15rpx;
			}
		}
		.addStore-list-item{
			width: 100%;
			height:88rpx;
			margin:0 auto;
			// padding:0 20rpx;
			box-sizing: border-box;
			// border-top:2rpx solid #F2F2F2;
			display: flex;
			align-items: center;
			justify-content: space-between;
			&-leftContent{
				width:40%;
				color:#007aff;
				font-weight: 600;
				font-size: 34rpx;
			}
			&-input{
				flex:1;
				text-align: right;
				color:#cccfd6;
				font-size: 30rpx;
				display: flex;
				align-items: center;
				flex-direction: row-reverse;
			}
		}
		.footerBtn{
			width:580rpx;
			margin:0 auto;
			padding:22rpx 0;
			background: #007aff;
			text-align: center;
			color: white;
			border-radius: 50rpx;
			margin-top:70rpx;
			margin-bottom:180rpx;
		}
	}
</style>